/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package commons.util;

import org.openide.util.NbBundle;

import java.util.logging.Logger;

/**
 * @author Susanta Tewari
 */
public class ExceptionUtil {

    /**
     * Constructs ...
     *
     */
    private ExceptionUtil() {}

    /**
     * A convenient method to return or create a logger using the following call.
     * <pre>
     *  Logger.getLogger(class.getName(), class.getPackage().getName()+ ".Log");
     * </pre>
     *
     * @param cls class for which the logger is created
     * @return logger for the specified class
     */
    public static Logger getLogger(Class<?> cls) {
        return Logger.getLogger(cls.getName(), cls.getPackage().getName() + ".Log");
    }

    /**
     * When testing concrete methods of abstract classes, a mock of the class does not implement the
     * abstract methods. Those methods should never be called. This is a convenience utility for
     * such cases.
     *
     * @return throws {@code UnsupportedOperationException} with a message: "Not intended to be
     *         supported. Should not have been called"
     */
    public static UnsupportedOperationException throwUnSupportedEx() {

        throw new UnsupportedOperationException(
            "Not intended to be supported. Should not have been called");
    }

    /**
     * When all cases of an enum are examined, the default case should throw an exception. This is a
     * convenience utility for such cases.
     *
     * @param <E> an enum type
     * @param enumType an enum constant
     * @return
     */
    public static <E extends Enum<E>> RuntimeException throwRuntimeExDefaultEnum(E enumType) {
        throw new RuntimeException("Unknown enum constant : " + enumType);
    }

    /**
     * Throws an instance of {@code NullPointerException} for {@code null} value of the specified {@code parameter}
     *
     * @param parameter argument parameter
     * @return throws a new instance of {@code NullPointerException}
     */
    public static NullPointerException throwIllegalArgExNull(String parameter) {

        final String msg = NbBundle.getMessage(ExceptionUtil.class, "Null_Argument", parameter);

        throw new NullPointerException(msg);
    }

    /**
     * Method description
     *
     *
     * @param cls
     * @param bundleKey
     *
     * @return
     */
    public static IllegalArgumentException throwIllegalArgEx(Class<?> cls, String bundleKey) {

        final String msg = NbBundle.getMessage(cls, bundleKey);

        throw new IllegalArgumentException(msg);
    }

    /**
     * Method description
     *
     *
     * @param cls
     * @param bundleKey
     * @param object
     *
     * @return
     */
    public static IllegalArgumentException throwIllegalArgEx(Class<?> cls, String bundleKey,
            Object object) {

        final String msg = NbBundle.getMessage(cls, bundleKey, object);

        throw new IllegalArgumentException(msg);
    }

    /**
     *
     * @param condition condition to throw the exception
     * @param msg exception message, if thrown
     * @return throws {@code IllegalArgumentException} if {@code condition} is true; else {@code null}
     */
    public static IllegalArgumentException throwArgEx(boolean condition, String msg) {

        if (condition) throw new IllegalArgumentException(msg);

        return null;
    }

    public static IllegalStateException throwStateEx(boolean condition, String msg) {

        if (condition) throw new IllegalArgumentException(msg);

        return null;
    }

    /**
     * Method description
     *
     *
     * @param cls
     * @param bundleKey
     * @param object
     *
     * @return
     */
    public static IllegalStateException throwIllegalStateEx(Class<?> cls, String bundleKey,
            Object object) {

        final String msg = NbBundle.getMessage(cls, bundleKey, object);

        throw new IllegalStateException(msg);
    }
}
